3 * This is the MySQLi database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace Wikimedia\Rdbms
;
31 * Database abstraction object for PHP extension mysqli.
37 class DatabaseMysqli
extends DatabaseMysqlBase
{
40 * @return mysqli_result
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
45 if ( $this->bufferResults() ) {
46 $ret = $conn->query( $sql );
48 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
55 * @param string $realServer
56 * @param string|null $dbName
58 * @throws DBConnectionError
60 protected function mysqlConnect( $realServer, $dbName ) {
61 # Avoid suppressed fatal error, which is very hard to track down
62 if ( !function_exists( 'mysqli_init' ) ) {
63 throw new DBConnectionError( $this, "MySQLi functions missing,"
64 . " have you compiled PHP with the --with-mysqli option?\n" );
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
72 $hostAndPort = IP
::splitHostAndPort( $realServer );
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 $hostAndSocket = explode( ':', $realServer, 2 );
82 $realServer = $hostAndSocket[0];
83 $socket = $hostAndSocket[1];
86 $mysqli = mysqli_init();
89 if ( $this->flags
& self
::DBO_SSL
) {
90 $connFlags |
= MYSQLI_CLIENT_SSL
;
99 if ( $this->flags
& self
::DBO_COMPRESS
) {
100 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
102 if ( $this->flags
& self
::DBO_PERSISTENT
) {
103 $realServer = 'p:' . $realServer;
106 if ( $this->utf8Mode
) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
115 if ( $mysqli->real_connect(
130 protected function connectInitCharset() {
131 // already done in mysqlConnect()
136 * @param string $charset
139 protected function mysqlSetCharset( $charset ) {
140 $conn = $this->getBindingHandle();
142 if ( method_exists( $conn, 'set_charset' ) ) {
143 return $conn->set_charset( $charset );
145 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
152 protected function closeConnection() {
153 $conn = $this->getBindingHandle();
155 return $conn->close();
161 function insertId() {
162 $conn = $this->getBindingHandle();
164 return (int)$conn->insert_id
;
170 function lastErrno() {
171 if ( $this->conn
instanceof mysqli
) {
172 return $this->conn
->errno
;
174 return mysqli_connect_errno();
181 protected function fetchAffectedRowCount() {
182 $conn = $this->getBindingHandle();
184 return $conn->affected_rows
;
187 function doSelectDomain( DatabaseDomain
$domain ) {
188 $conn = $this->getBindingHandle();
190 if ( $domain->getSchema() !== null ) {
191 throw new DBExpectedError( $this, __CLASS__
. ": domain schemas are not supported." );
194 $database = $domain->getDatabase();
195 if ( !$conn->select_db( $database ) ) {
196 throw new DBExpectedError( $this, "Could not select database '$database'." );
199 // Update that domain fields on success (no exception thrown)
200 $this->currentDomain
= $domain;
206 * @param mysqli_result $res
209 protected function mysqlFreeResult( $res ) {
216 * @param mysqli_result $res
217 * @return stdClass|bool
219 protected function mysqlFetchObject( $res ) {
220 $object = $res->fetch_object();
221 if ( $object === null ) {
229 * @param mysqli_result $res
232 protected function mysqlFetchArray( $res ) {
233 $array = $res->fetch_array();
234 if ( $array === null ) {
242 * @param mysqli_result $res
245 protected function mysqlNumRows( $res ) {
246 return $res->num_rows
;
250 * @param mysqli_result $res
253 protected function mysqlNumFields( $res ) {
254 return $res->field_count
;
258 * @param mysqli_result $res
262 protected function mysqlFetchField( $res, $n ) {
263 $field = $res->fetch_field_direct( $n );
265 // Add missing properties to result (using flags property)
266 // which will be part of function mysql-fetch-field for backward compatibility
267 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
268 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
269 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
270 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
271 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
272 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
273 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
274 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
275 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
281 * @param mysqli_result $res
285 protected function mysqlFieldName( $res, $n ) {
286 $field = $res->fetch_field_direct( $n );
292 * @param mysqli_result $res
296 protected function mysqlFieldType( $res, $n ) {
297 $field = $res->fetch_field_direct( $n );
303 * @param mysqli_result $res
307 protected function mysqlDataSeek( $res, $row ) {
308 return $res->data_seek( $row );
312 * @param mysqli|null $conn Optional connection object
315 protected function mysqlError( $conn = null ) {
316 if ( $conn === null ) {
317 return mysqli_connect_error();
324 * Escapes special characters in a string for use in an SQL statement
328 protected function mysqlRealEscapeString( $s ) {
329 $conn = $this->getBindingHandle();
331 return $conn->real_escape_string( (string)$s );
335 * Give an id for the connection
337 * mysql driver used resource id, but mysqli objects cannot be cast to string.
340 public function __toString() {
341 if ( $this->conn
instanceof mysqli
) {
342 return (string)$this->conn
->thread_id
;
344 // mConn might be false or something.
345 return (string)$this->conn
;
352 protected function getBindingHandle() {
353 return parent
::getBindingHandle();
358 * @deprecated since 1.29
360 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );